package com.zenghus.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class LoggerProxy implements MethodInterceptor{

    public Object newProxyInstance(Object obj){
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(obj.getClass());
        enhancer.setCallback(this);
        return enhancer.create();

    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib 开始调用"+method.getName()+"方法");
        Object ob=methodProxy.invokeSuper(o,objects);
        System.out.println("cglib"+method.getName()+"方法调用结束");
        return ob;
    }
}
